package com.app.controller;

import com.app.enums.StatusCodeEnum;
import com.app.model.JiuquanUserModel;
import com.app.service.UserService;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import com.app.vo.UserLoginVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.security.auth.login.LoginException;
import javax.servlet.http.HttpServletRequest;

//酒泉用户表
@RestController
@Controller
@RequestMapping("/jiuquan/user")
public class UserController {
    @Autowired
    private UserService userService;

    //获取openid
    @GetMapping("/getOpenid")
    public Result<UserLoginVo> getOpenid(String code, HttpServletRequest request) throws LoginException {
        //微信登录
        JiuquanUserModel user = userService.getOpenid(code, request);
        //生成token
        String token = TokenUtils.sign(user.getOpenId());
        UserLoginVo userLoginVO = UserLoginVo.builder()
                .user(user)
                .token(token)
                .build();
        System.out.println(token);
        return Result.success(userLoginVO);
    }

    //获取手机号
    @GetMapping("/getPhone")
    public Result<Object> getPhone(String code, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(StatusCodeEnum.STATUS405.getCode(), StatusCodeEnum.STATUS405.getMsg());
        }
        String openId = TokenUtils.verify(token);

        String phone = userService.getPhone(code, request);
        QueryWrapper<JiuquanUserModel> qw = new QueryWrapper<>();
        qw.eq("phone", phone);
        JiuquanUserModel userModel = userService.getOne(qw);
        if (userModel == null) {
            userModel = new JiuquanUserModel();
            userModel.setRealName("游客");
            userModel.setOpenId(openId);
            userModel.setFaceUrl("https://www.adhrsp.com/file/91885/img/Mr.jpg");
            userModel.setCrtTime(CommonHelp.getNowTimestamp());
            userModel.setMobile(phone);
            userModel.setStatus(1);
            userService.save(userModel);
        }
        return Result.success(userModel);
    }

    //新增和修改
    @PostMapping("/addAndUpdateUser")
    public Result<Object> addAndUpdateUser(@RequestBody JiuquanUserModel userModel, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(StatusCodeEnum.STATUS405.getCode(), StatusCodeEnum.STATUS405.getMsg());
        }
        String openId = TokenUtils.verify(token);
        JiuquanUserModel userInfo = userService.getUserByOpenid(openId);
        if (userInfo != null) {
            userModel.setUpTime(CommonHelp.getNowTimestamp());
            UpdateWrapper<JiuquanUserModel>up=new UpdateWrapper<>();
            up.eq("open_id",openId);
            userService.update(userModel,up);
            return Result.success("修改成功");
        } else {
            userModel.setOpenId(openId);
            userModel.setCrtTime(CommonHelp.getNowTimestamp());
            userService.save(userModel);
            return Result.success("添加成功");
        }
    }

    //小程序端登录后的查询个人信息
    @GetMapping("/getUserInfo")
    public Result<Object> getUserInfo(HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(StatusCodeEnum.STATUS405.getCode(), StatusCodeEnum.STATUS405.getMsg());
        }
        String openId = TokenUtils.nothingness(token);
        JiuquanUserModel user = userService.getUserByOpenid(openId);
        if (user == null) {
            return Result.error(StatusCodeEnum.STATUS406.getCode(), StatusCodeEnum.STATUS406.getMsg());
        }
        JiuquanUserModel userModel = userService.selectUserById(user.getId());
        userModel.setOpenId(null);
        return Result.success(userModel);
    }





}
